using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace HZY.Admin.Services
{
    using System.Threading.Tasks;
    using HZY.EFCore.Repository;
    using HZY.Models;
    using HZY.Models.Sys;
    using HZY.Toolkits;
    using HZY.EFCore.Base;
    using HZY.Admin.Services.Core;
    using HZY.EFCore;
    using Microsoft.EntityFrameworkCore;
    /// <summary>
    /// 客户
    /// </summary>

    public class BankAccountService : ServiceBase<BankAccount>
    {
        private readonly DefaultRepository<Customer> customerDb;
        public BankAccountService(EFCoreContext _db, DefaultRepository<Customer> customerDb) 
            : base(_db)
        {
            this.customerDb = customerDb;
        }

        #region CURD 基础

        /// <summary>
        /// 获取数据源
        /// </summary>
        /// <param name="Page"></param>
        /// <param name="Rows"></param>
        /// <param name="Search"></param>
        /// <returns></returns>
        public async Task<TableViewModel> FindListAsync(int Page, int Rows, BankAccount Search )
        {
            var query = (
                from bankaccount in this.db.BankAccounts
                from customer in this.db.Customers.Where(v => v.Customer_ID == bankaccount.BankAccount_CustomerID)
                select new { bankaccount, customer }
                )
               .WhereIF(w => w.bankaccount.BankAccount_Code.Contains(Search.BankAccount_Code), !string.IsNullOrWhiteSpace(Search?.BankAccount_Code))
                 .OrderBy(w => w.bankaccount.BankAccount_CreateTime)
                .Select(w => new
                {
                    w.bankaccount.BankAccount_Code,
                    w.bankaccount.BankAccount_Name,
                    w.customer.Customer_Name,
                    w.customer.Customer_Code,
                    w.bankaccount.BankAccount_Remarks,
                    _ukid = w.bankaccount.BankAccount_ID,
                    w.bankaccount.BankAccount_CreateTime
                });
            return await this.db.AsTableViewModelAsync(query, Page, Rows, typeof(BankAccount),typeof(Customer));
        }

        /// <summary>
        /// 新增\修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Guid> SaveAsync(BankAccount model)
        {
            await this.InsertOrUpdateAsync(model);

            return model.BankAccount_ID;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(List<Guid> Ids)
            => await this.DeleteAsync(w => Ids.Contains(w.BankAccount_ID));

        /// <summary>
        /// 加载表单 数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<Dictionary<string, object>> LoadFormAsync(Guid Id)
        {
            var res = new Dictionary<string, object>();

            var Model = await FindByIdAsync(Id);
            var customerInfo = await customerDb.Query().OrderBy(v => v.Customer_Code).ToListAsync();
            res[nameof(Id)] = Id;
            res[nameof(Model)] = Model.NullSafe();
            res[nameof(customerInfo)] = customerInfo;
            return res;
        }


        #endregion

        #region 导出 Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="Search"></param>
        /// <returns></returns>
        public async Task<byte[]> ExportExcel(BankAccount Search)
        {
            var tableViewModel = await this.FindListAsync(1, 999999, Search);
            return this.ExportExcelByTableViewModel(tableViewModel);
        }

        #endregion

    }
}
